package cn.edu.zjgsu.ito.service;

import java.util.List;

import cn.edu.zjgsu.ito.model.TeacherModel;

import com.jfinal.kit.EncryptionKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class TeaInfService extends Service{
	/*
	 * 张虎涛
	 * 2016/06/01
	 */
	public Record search(String tno){
		try{
			setStrSql(TeacherModel.dao.selectTeaInfoSql);
			
			setRecord(Db.findFirst(getStrSql(),tno));
			
			return getRecord();
		}
		catch (Exception e)
		{   		
			e.printStackTrace();
			return null;
		}		
	}
	public String TeaName(String tNo) {
		try {
			Record name;
			String teacher="";
			if(tNo.indexOf(",")==-1){
				name=Db.findFirst(TeacherModel.dao.selectTeaNameSql,tNo);
				teacher=name.getStr("tName");
			}else{
			      String[] strarray=tNo.split(" "); 
			      for (int i = 0; i < strarray.length; i++){
			    	  name=Db.findFirst(TeacherModel.dao.selectTeaNameSql,strarray[i]);
			    	  teacher+=name.getStr("tName");
			    	  if(i < strarray.length)teacher+=",";
			      } 
			     
			}
			
			return teacher;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean checkPassword(String name, String oldPassword) {

		oldPassword = EncryptionKit.md5Encrypt(oldPassword);// 加密

		boolean tf = false;

		try {
			setStrSql(TeacherModel.dao.checkTeaPasSql);

			setRecord(Db.findFirst(getStrSql(),name));

			tf = oldPassword.equals(getRecord().getStr("tPwd"));

			return tf;
		} catch (Exception e) {
			e.printStackTrace();
			return tf;
		}

	}
	
	public boolean updatePassword( String newPassword,String name) {

		newPassword = EncryptionKit.md5Encrypt(newPassword);// 加密

		try {
			setStrSql(TeacherModel.dao.updateTeaPasSql);
			Db.update(getStrSql(), newPassword, name);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public List<Record> allTeacher() {
		
		try {
			List<Record> list=Db.find(TeacherModel.dao.selectAllTeaSql);

			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
